/**
 * Created by zhoulin on 2019/4/3.
 */
var companyId = sessionStorageUtils.getJson(storageKey.sessionStorageKey.companyId);
var schoolId = sessionStorageUtils.getJson(storageKey.sessionStorageKey.schoolId);
var schoolMap = sessionStorageUtils.getJson(storageKey.sessionStorageKey.schoolMap);

var app = new Vue({
    el: '#app',
    data: {
        permissions: ['orderInfo:add', 'orderInfo:update', 'member:list'],//本页权限数组
        selects: [],
        members: [],
        desc: '',
        schoolId: schoolId,
        schoolList: schoolMap,
    },
    //进入页面初始化方法
    created: function () {
        var that = this;
        var token = sessionStorageUtils.getJson(storageKey.sessionStorageKey.token);
        if (empty(token)) {
            layer.msg("<span style='color:red'>登录超时！请重新登录！</span>", {time: 1500, icon: 5});
            setTimeout(function () {
                window.location.href = _LOGIN_PAGE;
            }, 1200);

            return;
        }
        if (!empty(aGET.id)) {
            setTimeout(function () {
                that.getInfoById(aGET.id);
            },200);
        }
    },
    methods: {
        //权限控制
        permission: function (index) {
            return (_PERMISSIONS.indexOf(this.permissions[index]) != -1);
        },
        // 选择进入不同页面选择
        selectOp: function() {
            var index = layer.open({
                type: 2,
                title: "选择商品",
                content: '../pages/goods.html',
                fix: true,
                shadeClose: true,
                shade: false,
                area: ['70%', '70%'],
                maxmin: true, //开启最大化最小化按钮
                end:function(){//关闭后调用的事件
                    var objectId = sessionStorageUtils.getJson(storageKey.sessionStorageKey.selectOp.objectId);
                    var objectName = sessionStorageUtils.getJson(storageKey.sessionStorageKey.selectOp.objectName);
                    $("#Form input[name='productId']").val(objectId);
                    $("#Form input[name='productName']").val(objectName);
                    sessionStorageUtils.del(storageKey.sessionStorageKey.selectOp.objectId);
                    sessionStorageUtils.del(storageKey.sessionStorageKey.selectOp.objectName);
                }
            });
            layer.full(index);
        },
        selectMember: function() {
            var index = layer.open({
                type: 2,
                title: "选择客户",
                content: '../pages/members.html',
                fix: true,
                shadeClose: true,
                shade: false,
                area: ['70%', '70%'],
                maxmin: true, //开启最大化最小化按钮
                end:function(){//关闭后调用的事件
                    var object = sessionStorageUtils.getJson(storageKey.sessionStorageKey.selectOp.object);
                    var objectId = sessionStorageUtils.getJson(storageKey.sessionStorageKey.selectOp.objectId);
                    var objectName = sessionStorageUtils.getJson(storageKey.sessionStorageKey.selectOp.objectName);
                    $("#Form input[name='memberId']").val(objectId);
                    $("#Form input[name='memberName']").val(objectName);
                    $("#Form input[name='memberPhone']").val(object.wx_tel);
                    sessionStorageUtils.del(storageKey.sessionStorageKey.selectOp.objectId);
                    sessionStorageUtils.del(storageKey.sessionStorageKey.selectOp.objectName);
                }
            });
            layer.full(index);
        },

        getInfoById: function (id) {
            var that = this;
            Get(that, _HOST + _API.user_orderInfo_getById + "?id=" + id)
                .then(function (response) {
                    // 响应成功回调
                    console.log(response);
                    if (response) {
                        if (response.success) {
                            var list = response.data;
                            for (var item in list) {
                                if (item == 'type') {
                                    $("#Form select[name='type']").val(list[item]);
                                }
                                if (item == 'memberId') {
                                    $("#Form select[name='memberId']").val(list[item]);
                                }
                                if (item == 'productId') {
                                    $("#Form select[name='productId']").val(list[item]);
                                }
                                $("#Form input[name='" + item + "']").val(list[item]);
                            }
                            $("#Form").data('bootstrapValidator').destroy();
                            $('#Form').data('bootstrapValidator', null);
                            Validator();
                        } else {
                            layer.msg(response.msg, {time: 1500, icon: 5});
                        }
                    }
                })
        },
        // 添加
        add: function () {
            var that = this;
            var param = formToJson($("#Form").serialize());
            param['companyId'] = companyId;
            param['schoolId'] = schoolId;
            for (let i = 0; i < that.selects.length; i++) {
                if (that.selects[i].id == param.productId) {
                    param['productName'] = that.selects[i].name;
                }
            }
            //console.log(param)
            Post(that, _HOST + _API.user_orderInfo_add, param, true, that.permissions[0])
                .then(function (response) {
                    // 响应成功回调
                    console.log(response);
                    if (response) {
                        if (response.success) {
                            layer.msg(response.msg, {time: 1500, icon: 1});
                            setTimeout(function () {
                                window.parent.location.reload(); //刷新父页面
                                layer.closeAll();
                            }, 1500)
                        } else {
                            layer.msg(response.msg, {time: 1500, icon: 5});
                        }
                    }
                    $("#Form").data('bootstrapValidator').destroy();
                    $('#Form').data('bootstrapValidator', null);
                    Validator();
                })
        },
        // 修改
        update: function () {
            var that = this;
            var param = formToJson($("#Form").serialize());
            param['companyId'] = companyId;
            for (let i = 0; i < that.selects.length; i++) {
                if (that.selects[i].id == param.productId) {
                    param['productName'] = that.selects[i].name;
                }
            }
            // console.log(param)
            Post(that, _HOST + _API.user_orderInfo_update, param, true, that.permissions[1])
                .then(function (response) {
                    // 响应成功回调
                    console.log(response);
                    if (response) {
                        if (response.success) {
                            layer.msg(response.msg, {time: 1500, icon: 1});
                            setTimeout(function () {
                                window.parent.location.reload(); //刷新父页面
                                layer.closeAll();
                            }, 1500)
                        } else {
                            layer.msg(response.msg, {time: 1500, icon: 5});
                        }
                    }
                    $("#Form").data('bootstrapValidator').destroy();
                    $('#Form').data('bootstrapValidator', null);
                    Validator();
                })
        },
    },
});


$('#Form').data('bootstrapValidator', null);
Validator();

function Validator() {
    $('#Form').bootstrapValidator({
        fields: {
            memberName: {
                validators: {
                    notEmpty: {
                        message: '请选择客户名字'
                    },
                }
            },
            memberPhone: {
                validators: {
                    notEmpty: {
                        message: '请选择客户电话'
                    },
                }
            },
            engineerName: {
                validators: {
                    notEmpty: {
                        message: '请输入技师名字'
                    },
                }
            }
        }
    }).on('success.form.bv', function (e) {
        // Prevent form submission
        e.preventDefault();
        if (empty(aGET.id)) {
            app.add();
        } else {
            app.update();
        }
    });
}
